package cn.candy.user.controller;

import cn.candy.basic.exception.BusinessExcpetion;
import cn.candy.basic.util.AjaxResult;
import cn.candy.basic.util.PageList;
import cn.candy.user.domain.User;
import cn.candy.user.dto.UserDto;
import cn.candy.user.query.UserQuery;
import cn.candy.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 用户接口类
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口类",description = "用户接口详细描述")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 查询所有对象接口：
     * @return - 返回查询的结果
     */
    @GetMapping
    @ApiOperation(value = "查询所有接口",notes = "")
    public List<User> findAll(){
        return userService.findAll();
    }

    /**
     * 查询单个对象接口
     * @param id - 传递查询对象的id
     * @return
     */
    @ApiOperation(value = "查询单个接口",notes = "需要传递id")
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id")Long id){
        return userService.findOne(id);
    }

    /**
     * 分页查询接口
     * @param userQuery - 分页参数 + 高级查询参数
     * @return 查询结果 - totals data
     */
    @ApiOperation(value = "分页查询接口",notes = "需要传递分页数据")
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery userQuery){
        return userService.queryPage(userQuery);
    }

    /**
     * 删除接口
     * @param id - 传递需要删除的id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口",notes = "需要传递id")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            userService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败，" + e.getMessage());
        }
    }
    /**
     * 添加或修改接口
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口",notes = "需要传递对象")
    public AjaxResult saveOrUpdate(@RequestBody User user){
        try {
            if(user.getId()==null){//添加操作
                userService.add(user);
            }else{
                userService.update(user);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败，" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids - 需要传递删除对象的id
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口",notes = "需要传递删除对象的id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            userService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("批量删除失败，" + e.getMessage());
        }
    }


    /**
     * 用户注册接口
     * @return
     */
    @PostMapping("/register/phone")
    @ApiOperation(value = "用户注册接口",notes = "")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto){
        try {
            userService.phoneRegister(userDto);
            return new AjaxResult();
        } catch (BusinessExcpetion e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("注册失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误，" + e.getMessage());
        }
    }
}
